<!-- 出入库明细 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-select v-model="queryParams.materielId" class="zs-select-md zs-space-right-sm" placeholder="清单名称" clearable @change="queryList">
					<el-option v-for="item in inventoryList" :key="item.id" :label="item.inventoryName" :value="item.id" />
				</el-select>
				<el-button class="zs-btn-plain" @click="queryList" v-btnPermi="{ permis: ['p_btn_property_project_device_list_query'] }"> 查询 </el-button>
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="inventoryName" label="清单名称" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="materielName" label="物料名称" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="specificationType" label="规格型号" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="brand" label="物料品牌" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="operationTypeName" label="操作" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="wayTypeName" label="方式" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="num" label="数量" show-overflow-tooltip align="right" :formatter="defaultTableData" />
				<el-table-column prop="inventoryNum" label="剩余库存" show-overflow-tooltip align="right" :formatter="defaultTableData" />
				<el-table-column prop="operationUserNameValue" label="入库/出库人" minWidth="100" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="formatOperationTime" label="入库/出库时间" minWidth="145" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="createUserName" label="操作人" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="formatCreateTime" label="操作时间" minWidth="145" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column label="审核状态" width="120" align="center">
					<template #default="scope">
						<el-button class="zs-link" link size="small" v-btnPermi="{ permis: ['p_btn_property_project_device_list_edit'], businessFlag: true }">{{ scope.row.auditStatusValue }}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>
</template>

<script setup>
	import { storageInOutInfoListApi, inventoryListByProjectApi } from '@/api/supplies.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRouter, useRoute } from 'vue-router';
	import { defaultTableData, getDictoryTxt, parseTime } from '@/utils/publicUtils';

	const router = useRouter();
	const route = useRoute();
	let store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ materielId: Number(route.query.id), pageNumber: 1, pageSize: curPageSize.value});

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_project_device_list_query')) return;
		}
		tableData.loading = true;
		let ids = inventoryList.value.map(item => item.id)
		queryParams.materielIdList = queryParams.materielId ? [ queryParams.materielId ] : ids
		let res = await storageInOutInfoListApi(queryParams);
		if (res.data.code !== 200) return;
		initData(res.data.data.list);
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	const initData = list => {
		list.forEach(item => {
			item.operationTypeName = item.operationType == 1 ? '出库' : '入库';
			item.wayTypeName = item.wayType == 1 ? '手动' : '自动';
			item.formatOperationTime = parseTime(item.operationTime, '{y}-{m}-{d} {h}:{i}');
			item.formatCreateTime = parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}');
		});
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	//清单名称
	let inventoryList = ref([])
	const getInventoryList = async() => {
		let res = await inventoryListByProjectApi({projectCode: route.query.projectCode})
		inventoryList.value = res.data.data
	}

	queryList('init');
	getInventoryList();
</script>
